package com.jhhc.WaterQuality.Calculate.Forecast.service.impl;


import com.jhhc.StormSurgeForecast.StormSurge.Pojo.GZABO.CalculationResults;
import com.jhhc.StormSurgeForecast.StormSurge.Pojo.GZABO.ColourInfo;
import com.jhhc.StormSurgeForecast.StormSurge.Pojo.GZABO.Result;
import com.jhhc.StormSurgeForecast.StormSurge.Uitl.ColourUtil;
import com.jhhc.StormSurgeForecast.StormSurge.Uitl.JudgeUtil;
import com.jhhc.WaterQuality.Calculate.Forecast.dao.BDMS_WQ_RESULT_MMapper;
import com.jhhc.WaterQuality.Calculate.Forecast.entity.BDMS_WQ_RESULT_M;
import com.jhhc.WaterQuality.Calculate.Forecast.service.BDMS_WQ_RESULT_MService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * (BdmsWqResultM)表服务实现类
 *
 * @author makejava
 * @since 2023-08-18 11:28:07
 */
@Service
public class BDMS_WQ_RESULT_MServiceImpl implements BDMS_WQ_RESULT_MService {
    @Resource
    private BDMS_WQ_RESULT_MMapper BDMSWQRESULTMMapper;


    @Override
    public Result getCalResultsByDomain(String calSchemeId, String startTime, String endTime, int dttp, boolean isData) {
        List<BDMS_WQ_RESULT_M> bdmsWqResultMList = BDMSWQRESULTMMapper.
                getCalResultsByDomain(startTime, endTime, calSchemeId, dttp);
        if (bdmsWqResultMList != null && bdmsWqResultMList.size() > 0) {
            Result result = new Result();
            List<CalculationResults> calculationResult11s = new ArrayList<>();
            CalculationResults calculation = null;
            for (BDMS_WQ_RESULT_M bdmsWqResultM : bdmsWqResultMList) {
                calculation = new CalculationResults();
                calculation.setTime(bdmsWqResultM.getDttm());
                String[] strings = bdmsWqResultM.getDatavalues().split("(?<=\\G.{" + 8 + "})");
                if (isData) {
                    int[] ints = Arrays.stream(strings).mapToInt(Integer::parseInt).toArray();
                    calculation.setZ(ints);
                }else {
                    calculation.setZColour(ColourUtil.
                            colourClassification(dttp,strings,result.getColourInfo()));
                }
                calculationResult11s.add(calculation);
            }
            result.setCalculationResults(calculationResult11s);
            result.setMultiple(100000);
            List<ColourInfo> colourInfos = new ArrayList<>();
            ColourInfo colourInfo = null;
            String[] colours = new String[]{"255,0,0","255,85,0","255,170,0","255,255,0","170,255,0",
                    "85,255,0","0,255,0","0,226,55","0,196,110","0,166,166","0,111,195","0,56,225","0,0,255",
                    "42,0,213","198,198,255","255,255,255"};
            double min = 0.15;
            double max = 2.25;
            double grid = (max + min) / 16;
            double scope = max;
            double scope1 = 0.15;
            for (int i = 0; i < 16; i++) {
                colourInfo = new ColourInfo();
                colourInfo.setSort(i + 1);
                colourInfo.setColourRGB(colours[i]);
                if (i == 0) {
                    colourInfo.setScope(String.valueOf(max));
                    scope1 = max;
                }else if (i == 15) {
                    colourInfo.setScope(String.valueOf(min));
                }else {
                    scope = scope - grid;
                    colourInfo.setScope(String.format("%.2f",scope1) + "," + String.format("%.2f",scope));
                    scope1 = scope;
                }
                colourInfos.add(colourInfo);
            }
            result.setColourInfo(colourInfos);
            return result;
        }else {
            return null;
        }
    }
}
